﻿using System;
using BusinessBingo.Web.Infrastructure.InfoStores.Interfaces;
using BusinessBingo.Web.Infrastructure.InfoStores.Keys;

namespace BusinessBingo.Web.Infrastructure.InfoStores
{
    /// <summary>
    /// Access to VersionInfo of the webapplication - cached
    /// </summary>
    public class VersionStore
    {
        /// <summary>
        /// CacheRepository for storing version info
        /// </summary>
        private ICacheRepository _cacheRepo;

        /// <summary>
        /// Ctor with dependencies
        /// </summary>
        /// <param name="cacheRepository">Implementation of ICacheRepository is needed</param>
        public VersionStore(ICacheRepository cacheRepository)
        {
            this._cacheRepo = cacheRepository;
        }

        /// <summary>
        /// Returns the current version info - get the version from the cache. If the cache is empty, the current assemblyversion will be stored
        /// </summary>
        /// <returns></returns>
        public Version GetVersion()
        {
            if (this._cacheRepo.HasElement<Version>(CacheKeys.Version))
            {
                return this._cacheRepo.GetElement<Version>(CacheKeys.Version);
            }
            else
            {
                Version actualVersion = this.GetType().Assembly.GetName().Version;
                this._cacheRepo.AddElement<Version>(CacheKeys.Version, actualVersion);
                return actualVersion;
            }
        }
    }
}